/**
 * 麻将测试
 * */
var TestMahjong = cc.Layer.extend({
    _paiType: {
        Wan: 'wan',
        Tiao: 'tiao',
        Tong: 'tong',
        Feng: 'feng'
    },
    _type: null,
    _curPos: 0,
    _numObj: {},
    ctor: function () {
        this._super();
        this._type = this._paiType.Wan;
        loadFile(this, majiang.res.TestMahjong_json);
        this.rootNode.x = cc.winSize.width / 2;
        this.rootNode.y = cc.winSize.height / 2;

        this._initView();
        this._addTouchListener();
        this._refreshSet();
    },

    _initView: function () {
        for (var i = 0; i < 5; i++) {
            for (var j = 0; j < 14; j++) {
                safeHide(this['_pos' + i]['card' + j]);
            }
            this._numObj['_pos' + i] = [];
        }
    },

    _pushPai: function (index) {
        var id = 0;
        switch (this._type) {
            case this._paiType.Wan:
                id = 1 + index;
                break;
            case this._paiType.Tiao:
                // id = 10 + index;
                id = 11 + index;
                break;
            case this._paiType.Tong:
                // id = 19 + index;
                id = 21 + index;
                break;
            case this._paiType.Feng:
                // id = 28 + index;
                id = 31 + index;
                break;
        }
        if (id > 0 && this._numObj['_pos' + this._curPos].length < 14) {
            this._numObj['_pos' + this._curPos].push(id);
            // cc.log(this._numObj.toString());
            this._refreshView();
        }
    },
    _pushPaiByID: function (id) {
        if (id > 0 && this._numObj['_pos' + this._curPos].length < 14) {
            this._numObj['_pos' + this._curPos].push(id);
            // cc.log(this._numObj.toString());
            this._refreshView();
        }
    },

    _refreshSet: function () {
        for (var i = 0; i < 9; i++) {
            // 1万 p2l1.png
            // 1条 p2l10.png
            // 1筒 p2l19.png
            var id = 0;
            switch (this._type) {
                case this._paiType.Wan:
                    id = 1 + i;
                    break;
                case this._paiType.Tiao:
                    id = 10 + i;
                    break;
                case this._paiType.Tong:
                    id = 19 + i;
                    break;
                case this._paiType.Feng:
                    id = 28 + i;
                    break;
            }
            this['_set']['card' + i].setVisible(false);
            var frame = cc.spriteFrameCache.getSpriteFrame('p2l' + id + '.png');
            if (frame) {
                this['_set']['card' + i].setSpriteFrame(frame);
                this['_set']['card' + i].setVisible(true);
            }
        }
    },

    _setPai: function (type, idx) {
        var frame = cc.spriteFrameCache.getSpriteFrame(name);
        // 1万 p2l1.png
        // 1条 p2l10.png
        // 1筒 p2l19.png
    },

    /**
     * 添加触摸事件
     * @private
     * */
    _addTouchListener: function () {
        var self = this;
        TouchUtil.setOnclickListener(this['_btn_close'], function () {
            self.removeFromParent();
        });
        TouchUtil.setOnclickListener(this['_btn_wan'], function () {
            self._type = self._paiType.Wan;
            self._refreshSet();
        });
        TouchUtil.setOnclickListener(this['_btn_tong'], function () {
            self._type = self._paiType.Tong;
            self._refreshSet();
        });
        TouchUtil.setOnclickListener(this['_btn_tiao'], function () {
            self._type = self._paiType.Tiao;
            self._refreshSet();
        });
        TouchUtil.setOnclickListener(this['_btn_feng'], function () {
            self._type = self._paiType.Feng;
            self._refreshSet();
        });
        TouchUtil.setOnclickListener(this['_btn_send'], function () {
            var data = {};
            data['paiArrs'] = [];
            for (var s = 0; s < 4; s++) {
                data['paiArrs'].push({
                    pos: s, paiArr: self._numObj['_pos' + s]
                });
            }

            data['unusedPaiArr'] = self._numObj['_pos4'];
            data['room_id'] = gameData.roomId;
            console.log('配牌器：' + JSON.stringify(data));
            network.send(3999, data);
            self.removeFromParent();
        });
        for (var idx = 0; idx < 9; idx++) {
            (function (idx) {
                TouchUtil.setOnclickListener(self['_set']['card' + idx], function () {
                    self._pushPai(idx);
                });
            })(idx);
        }
        for (var index = 0; index < 5; index++) {
            (function (index) {
                TouchUtil.setOnclickListener(self['_pos' + index]['set'], function () {
                    self._curPos = index;
                });
                TouchUtil.setOnclickListener(self['_pos' + index]['delete'], function () {
                    self._numObj['_pos' + index].pop();
                    self._refreshView();
                });
            })(index);
        }

        TouchUtil.setOnclickListener(self['_btn_input_ok'], function () {
            var str = self['_input'].getString()
            var arr = str.split(',')
            for (var i=0; i<arr.length; i++) {
                self._pushPaiByID(parseInt(arr[i]));
            }
        });
    },

    _refreshView: function () {
        var self = this;
        for (var m = 0; m < 5; m++) {
            for (var n = 0; n < 14; n++) {
                safeHide(this['_pos' + m]['card' + n]);
            }
        }
        for (var key in this._numObj) {
            if (this._numObj.hasOwnProperty(key)) {
                var cards_arr = this._numObj[key];
                // console.log(key + '=-------', cards_arr);
                for (var i = 0; i < cards_arr.length; i++) {
                    (function (i) {
                        // 1万 p2l1.png
                        // 1条 p2l10.png
                        // 1筒 p2l19.png
                        var id = cards_arr[i];
                        var name = '';
                        if (id >= 1 && id <= 9) {
                            name = 'p2l' + id + '.png';
                        } else if (id >= 11 && id <= 19) {
                            name = 'p2l' + (id - 1) + '.png';
                        } else if (id >= 21 && id <= 29) {
                            name = 'p2l' + (id - 2) + '.png';
                        } else if (id >= 31 && id <= 37) {
                            name = 'p2l' + (id - 3) + '.png';
                        }
                        var frame = cc.spriteFrameCache.getSpriteFrame(name);
                        if (frame) {
                            self[key]['card' + i].setSpriteFrame(frame);
                            self[key]['card' + i].setVisible(true);
                        }
                    })(i);
                }
            }
        }
    }
});